﻿#region Header
// Copyright 2007-2011, Jerónimo Milea
// 
// This file is part of NetJsWire.
// 
//     NetJsWire is free software: you can redistribute it and/or modify
//     it under the terms of the GNU General Public License as published by
//     the Free Software Foundation, either version 3 of the License, or
//     (at your option) any later version.
// 
//     NetJsWire is distributed in the hope that it will be useful,
//     but WITHOUT ANY WARRANTY; without even the implied warranty of
//     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//     GNU General Public License for more details.
// 
//     You should have received a copy of the GNU General Public License
//     along with NetJsWire.  If not, see <http://www.gnu.org/licenses/>.
#endregion

using System;
using System.ComponentModel;
using System.Windows.Forms;

namespace NetJsWire.Widgets.Ui.Table
{
    ///<summary>
    ///  A simple <see cref = "DataGridViewTextBoxColumn" /> with added properties.
    ///</summary>
    public class NetJsWireGridTextColumn : DataGridViewTextBoxColumn, INetJsWireGridColumn
    {
        private string _css = string.Empty;
        private string _editor = string.Empty;
        private bool _fixed;
        private bool _hideAble = true;
        private string _id = string.Empty;
        private string _renderer = string.Empty;

        /// <summary>
        ///   Gets or sets a value indicating whether the column is visible.
        /// </summary>
        public override bool Visible
        {
            get { return base.Visible; }
            set
            {
                base.Visible = value;

                if ( DataGridView != null && ( DataGridView as Table ).Renderer.Rendered )
                {
                    if ( !( DataGridView as Table ).IsInternalChange )
                    {
                        ( DataGridView as Table ).Renderer.AddCommand( "getColumnModel().setHidden",
                                                                       new object[] {Index + 1, !value} );
                    }
                }
            }
        }

        /// <summary>
        ///   Gets or sets the header text of the Column.
        /// </summary>
        public new string HeaderText
        {
            get { return base.HeaderText; }
            set
            {
                base.HeaderText = value;
                if ( DataGridView != null && ( DataGridView as Table ).Renderer.Rendered )
                {
                    ( DataGridView as Table ).Renderer.AddCommand( "getColumnModel().setColumnHeader",
                                                                   new object[] {Index + 1, value} );
                }
            }
        }

        /// <summary>
        ///   Gets or sets the header text of the Column.
        /// </summary>
        public new string ToolTipText
        {
            get { return base.ToolTipText; }
            set
            {
                base.ToolTipText = value;
                if ( DataGridView != null && ( DataGridView as Table ).Renderer.Rendered )
                {
                    ( DataGridView as Table ).Renderer.AddCommand( "getColumnModel().setColumnTooltip",
                                                                   new object[] {Index + 1, value} );
                }
            }
        }

        #region INetJsWireGridColumn Members

        /// <summary>
        ///   Set custom CSS for all table cells in the column (excluding headers). Defaults to undefined.
        /// </summary>
        /// <exception cref = "System.InvalidOperationException">Thrown when try to set the value after render.</exception>
        public string Css
        {
            set
            {
                if ( DataGridView != null && ( DataGridView as Table ).Renderer.Rendered )
                {
                    throw new InvalidOperationException( "You cannot set this value at runtime." );
                }
                _css = value;
            }
            get { return _css; }
        }

        /// <summary>
        ///   Gets or sets a value indicating whether the column is visible.
        /// </summary>
        [ DefaultValue( true ) ]
        public bool HideAble
        {
            get { return _hideAble; }
            set
            {
                _hideAble = value;

                if ( DataGridView != null && ( DataGridView as Table ).Renderer.Rendered )
                {
                    if ( !( DataGridView as Table ).IsInternalChange )
                    {
                        ( DataGridView as Table ).Renderer.AddCommand( "getColumnModel().setHideAble",
                                                                       new object[] {Index + 1, value} );
                    }
                }
            }
        }

        /// <summary>
        ///   (optional) Defaults to the column's initial ordinal position. A name which identifies this column. The id is used to create a CSS class name which is applied to all table cells (including headers) in that column. The class name takes the form of 
        ///   <code>x-grid3-td-id</code>
        ///   Header cells will also recieve this class name, but will also have the class x-grid3-hd, so to target header cells, use CSS selectors such as:
        ///   <code>.x-grid3-hd.x-grid3-td-id</code>
        /// </summary>
        /// <exception cref = "System.InvalidOperationException">Thrown when try to set the value after render.</exception>
        public string Id
        {
            set
            {
                if ( DataGridView != null && ( DataGridView as Table ).Renderer.Rendered )
                {
                    throw new InvalidOperationException( "You cannot set this value at runtime." );
                }
                _id = value;
            }
            get { return _id; }
        }

        /// <summary>
        ///   (optional) The Ext.form.Field to use when editing values in this column if editing is supported by the grid.
        /// </summary>
        /// <exception cref = "System.InvalidOperationException">Thrown when try to set the value after render.</exception>
        public string Editor
        {
            set
            {
                if ( DataGridView != null && ( DataGridView as Table ).Renderer.Rendered )
                {
                    throw new InvalidOperationException( "You cannot set this value at runtime." );
                }
                _editor = value;
            }
            get { return _editor; }
        }

        /// <summary>
        ///   (optional) A callback (or inline) function that renders the content on the cell.
        /// </summary>
        /// <exception cref = "System.InvalidOperationException">Thrown when try to set the value after render.</exception>
        public string Renderer
        {
            set
            {
                if ( DataGridView != null && ( DataGridView as Table ).Renderer.Rendered )
                {
                    throw new InvalidOperationException( "You cannot set this value at runtime." );
                }
                _renderer = value;
            }
            get { return _renderer; }
        }

        /// <summary>
        ///   If true the column wont be shown in the grid nor in the header menu..
        /// </summary>
        /// <exception cref = "System.InvalidOperationException">Thrown when try to set the value after render.</exception>
        public bool Fixed
        {
            set
            {
                if ( DataGridView != null && ( DataGridView as Table ).Renderer.Rendered )
                {
                    throw new InvalidOperationException( "You cannot set this value at runtime." );
                }
                _fixed = value;
            }
            get { return _fixed; }
        }

        #endregion
    }
}